package de.lusiardi.webdav.headers;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * {@link  http://www.webdav.org/specs/rfc4918.html#HEADER_Overwrite}
 *
 * @author shing19m
 */
public class OverwriteHeader implements Header {

    private static final Logger logger = Logger.getLogger(OverwriteHeader.class.getCanonicalName());

    public static final String KEY = "Overwrite";

    private String value;

    public String getKey() {
        return KEY;
    }

    public void setValue(String val) {
        value = val;
        if (!checkValue()) {
            logger.warning(String.format("'%s' is an invalid value for Timeout header.", value));
        }
    }

    public String getValue() {
        return value;
    }

    /**
     * As defined in http://www.webdav.org/specs/rfc4918.html#HEADER_Overwrite
     * <pre>Overwrite = "Overwrite" ":" ("T" | "F")</pre>
     *
     * @return
     */
    public boolean checkValue() {
        if (value == null) {
            return false;
        }
        //HTTP/1.1 403 Forbidden
        Pattern pattern = Pattern.compile("^(T|F)$");
        Matcher dateMatcher = pattern.matcher(value);

        if (!dateMatcher.matches()) {
            return false;
        }
        return true;
    }
}
